package com.hawthorn.redis.service.role.impl;

import com.hawthorn.component.constant.RedisConstant;
import com.hawthorn.component.utils.common.StrMyUtil;
import com.hawthorn.entity.dto.SysRoleDTO;
import com.hawthorn.entity.po.SysRolePO;
import com.hawthorn.platform.redis.RedisMyClient;
import com.hawthorn.platform.utils.common.MyUtil;
import com.hawthorn.redis.repository.RoleCacheMapper;
import com.hawthorn.redis.service.role.RoleCacheService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>todo 加载角色缓存服务实现类 </p>
 * @author: andyten@tom.com | xukaixing@hotmail.com
 * @created: 2021/11/26 12:32 PM
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Service
@Slf4j
public class RoleCacheServiceImpl implements RoleCacheService
{
  @Resource
  RedisMyClient redisMyClient;

  @Resource
  private RoleCacheMapper roleCacheMapper;

  @Transactional(readOnly = true)
  public void init()
  {
    List<SysRoleDTO> roleCache = roleCacheMapper.selectRole();
    String key;
    // todo 先删除redis中的key
    redisMyClient.removeAll(StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_ROLE_PREFIX, "*"));

    for (SysRoleDTO sysRoleDTO : roleCache)
    {
      key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_ROLE_PREFIX, sysRoleDTO.getId());

      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_RCODE, sysRoleDTO.getRcode());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_RNAME, sysRoleDTO.getRname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_ENAME, sysRoleDTO.getEname());
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_LEVEL, MyUtil.getValue(sysRoleDTO.getLevel()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_TYPE, MyUtil.getValue(sysRoleDTO.getType()));
      redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_DEPTID, MyUtil.getValue(sysRoleDTO.getDeptId()));
    }

    log.info("====== load roledata to redis success ======");
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void saveRole(SysRolePO sysRolePO)
  {
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_ROLE_PREFIX, MyUtil.getValue(sysRolePO.getId()));

    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_RCODE, MyUtil.getValue(sysRolePO.getRcode()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_RNAME, MyUtil.getValue(sysRolePO.getRname()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_ENAME, MyUtil.getValue(sysRolePO.getEname()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_LEVEL, MyUtil.getValue(sysRolePO.getLevel()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_TYPE, MyUtil.getValue(sysRolePO.getType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_DEPTID, MyUtil.getValue(sysRolePO.getDeptId()));

    log.info("====== insert roledata to redis success ======");
  }

  @Override
  @Transactional(rollbackFor = Exception.class)
  public void updateRole(SysRolePO sysRolePO)
  {
    // todo: 更新角色缓存
    String key = StrMyUtil.placeHolder(RedisConstant.REDIS_KEY_ROLE_PREFIX, MyUtil.getValue(sysRolePO.getId()));

    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_RCODE, MyUtil.getValue(sysRolePO.getRcode()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_RNAME, MyUtil.getValue(sysRolePO.getRname()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_ENAME, MyUtil.getValue(sysRolePO.getEname()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_LEVEL, MyUtil.getValue(sysRolePO.getLevel()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_TYPE, MyUtil.getValue(sysRolePO.getType()));
    redisMyClient.hSet(key, RedisConstant.REDIS_KEY_ROLE_DEPTID, MyUtil.getValue(sysRolePO.getDeptId()));

    log.info("====== update role data to redis successfully ======");
  }

}
